﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using tenpay;
using Com.Alipay;

public partial class PaymentOnLine_PaymentInterface : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string type = GZ.Common.HttpHelper.GetParameter("Type", GZ.Common.HttpHelper.HttpType.GET);
            switch (type)
            {
                case "财付通":
                    TenPay();
                    break;
                case "支付宝":
                    AliPay();
                    break;
            }
        }
    }

    void TenPay()
    {
        //商户号
        string partner = "1213901601";
        //密钥
        string key = "f0ad927b45b003affca30a545b5132f3";
        //当前时间 yyyyMMdd
        string date = DateTime.Now.ToString("yyyyMMdd");
        //订单号，此处用时间和随机数生成，商户根据自己调整，保证唯一
        string out_trade_no = GZ.Common.HttpHelper.GetParameter("OrderId", GZ.Common.HttpHelper.HttpType.GET);
        //创建RequestHandler实例
        RequestHandler reqHandler = new RequestHandler(Context);
        //初始化
        reqHandler.init();
        //设置密钥
        reqHandler.setKey(key);
        reqHandler.setGateUrl("https://gw.tenpay.com/gateway/pay.htm");

        string tempAmount = (Convert.ToInt32(GZ.Common.HttpHelper.GetParameter("OrderAmount", GZ.Common.HttpHelper.HttpType.GET)) * 100).ToString();
        //tempAmount = tempAmount.Substring(0, tempAmount.Length - 3);
        //支付日志
        string PaymentLog = string.Empty;


        //设置支付参数
        //-----------------------------
        reqHandler.setParameter("total_fee", tempAmount);
        PaymentLog += "total_fee:" + tempAmount + ";";
        //用户的公网ip,测试时填写127.0.0.1,只能支持10分以下交易
        reqHandler.setParameter("spbill_create_ip", Page.Request.UserHostAddress);
        PaymentLog += "spbill_create_ip:" + Page.Request.UserHostAddress + ";";
        reqHandler.setParameter("return_url", "http://www.guozhi321.com/PaymentOnLine/PaymentOK.aspx");
        PaymentLog += "return_url:" + "http://www.guozhi321.com/PaymentOnLine/PaymentOK.aspx" + ";";
        //reqHandler.setParameter("return_url", "");
        reqHandler.setParameter("partner", partner);
        PaymentLog += "partner:" + partner + ";";
        reqHandler.setParameter("out_trade_no", out_trade_no);
        PaymentLog += "out_trade_no:" + out_trade_no + ";";
        reqHandler.setParameter("notify_url", "http://www.guozhi321.com/PaymentOnLine/PaymentOK.aspx");
        PaymentLog += "notify_url:" + "http://www.guozhi321.com/PaymentOnLine/PaymentOK.aspx" + ";";
        reqHandler.setParameter("attach", "123");
        PaymentLog += "attach:" + "123" + ";";
        reqHandler.setParameter("body", GZ.Common.HttpHelper.GetParameter("ProductName", GZ.Common.HttpHelper.HttpType.GET));
        PaymentLog += "body:" + GZ.Common.HttpHelper.GetParameter("ProductName", GZ.Common.HttpHelper.HttpType.GET) + ";";
        reqHandler.setParameter("bank_type", "DEFAULT");
        PaymentLog += "bank_type:" + "DEFAULT" + ";";
        //系统可选参数
        reqHandler.setParameter("sign_type", "MD5");
        PaymentLog += "sign_type:" + "MD5" + ";";
        reqHandler.setParameter("service_version", "1.0");
        PaymentLog += "service_version:" + "1.0" + ";";
        reqHandler.setParameter("input_charset", "UTF-8");
        PaymentLog += "input_charset:" + "UTF-8" + ";";
        reqHandler.setParameter("sign_key_index", "1");
        PaymentLog += "sign_key_index:" + "1" + ";";

        //业务可选参数
        /*
        reqHandler.setParameter("attach", "");
        reqHandler.setParameter("product_fee", "");
        reqHandler.setParameter("transport_fee", "");
        reqHandler.setParameter("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
        reqHandler.setParameter("time_expire", "");       
        reqHandler.setParameter("buyer_id", "");
        reqHandler.setParameter("goods_tag", "");
        reqHandler.setParameter("agentid", "");
        reqHandler.setParameter("agent_type", "");
         */
        //获取请求带参数的url
        string requestUrl = reqHandler.getRequestURL();

        //Get的实现方式
        string a_link = "<a target=\"_blank\" href=\"" + requestUrl + "\">" + "财付通支付" + "</a>";

        GZ.Entity.EPaymentLog obj = new GZ.Entity.EPaymentLog();
        obj.InsertLog(2, PaymentLog, Convert.ToInt64(out_trade_no));


        Response.Redirect(requestUrl);
        //Response.Write(a_link);

    }


    void AliPay()
    {
        ////////////////////////////////////////////请求参数////////////////////////////////////////////

        //必填参数//
        //支付日志
        string PaymentLog = string.Empty;

        //请与贵网站订单系统中的唯一订单号匹配
        string out_trade_no = GZ.Common.HttpHelper.GetParameter("OrderId", GZ.Common.HttpHelper.HttpType.GET);
        PaymentLog += "out_trade_no:" + out_trade_no + ";";
        //订单名称，显示在支付宝收银台里的“商品名称”里，显示在支付宝的交易管理的“商品名称”的列表里。
        string subject = GZ.Common.HttpHelper.GetParameter("ProductName", GZ.Common.HttpHelper.HttpType.GET);
        PaymentLog += "subject:" + subject + ";";
        //订单描述、订单详细、订单备注，显示在支付宝收银台里的“商品描述”里
        string body = GZ.Common.HttpHelper.GetParameter("ProductName", GZ.Common.HttpHelper.HttpType.GET);
        PaymentLog += "body:" + body + ";";
        //订单总金额，显示在支付宝收银台里的“应付总额”里
        string total_fee = (Convert.ToInt32(GZ.Common.HttpHelper.GetParameter("OrderAmount", GZ.Common.HttpHelper.HttpType.GET))).ToString();
        PaymentLog += "total_fee:" + total_fee + ";";


        //扩展功能参数——默认支付方式//

        //默认支付方式，代码见“即时到帐接口”技术文档
        string paymethod = "";
        //默认网银代号，代号列表见“即时到帐接口”技术文档“附录”→“银行列表”
        string defaultbank = "";

        //扩展功能参数——防钓鱼//

        //防钓鱼时间戳
        string anti_phishing_key = "";
        //获取客户端的IP地址，建议：编写获取客户端IP地址的程序
        string exter_invoke_ip = "";
        //注意：
        //请慎重选择是否开启防钓鱼功能
        //exter_invoke_ip、anti_phishing_key一旦被设置过，那么它们就会成为必填参数
        //建议使用POST方式请求数据
        //示例：
        //exter_invoke_ip = "";
        //Service aliQuery_timestamp = new Service();
        //anti_phishing_key = aliQuery_timestamp.Query_timestamp();               //获取防钓鱼时间戳函数

        //扩展功能参数——其他//

        //商品展示地址，要用http:// 格式的完整路径，不允许加?id=123这类自定义参数
        string show_url = "http://www.xxx.com/myorder.aspx";
        //自定义参数，可存放任何内容（除=、&等特殊字符外），不会显示在页面上
        string extra_common_param = "";
        //默认买家支付宝账号
        string buyer_email = "";

        //扩展功能参数——分润(若要使用，请按照注释要求的格式赋值)//

        //提成类型，该值为固定值：10，不需要修改
        string royalty_type = "";
        //提成信息集
        string royalty_parameters = "";
        //注意：
        //与需要结合商户网站自身情况动态获取每笔交易的各分润收款账号、各分润金额、各分润说明。最多只能设置10条
        //各分润金额的总和须小于等于total_fee
        //提成信息集格式为：收款方Email_1^金额1^备注1|收款方Email_2^金额2^备注2
        //示例：
        //royalty_type = "10";
        //royalty_parameters = "111@126.com^0.01^分润备注一|222@126.com^0.01^分润备注二";

        ////////////////////////////////////////////////////////////////////////////////////////////////

        //把请求参数打包成数组
        SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
        sParaTemp.Add("payment_type", "1");
        PaymentLog += "payment_type:" + "1" + ";";
        sParaTemp.Add("show_url", show_url);
        PaymentLog += "show_url:" + show_url + ";";
        sParaTemp.Add("out_trade_no", out_trade_no);
        sParaTemp.Add("subject", subject);
        sParaTemp.Add("body", body);
        sParaTemp.Add("total_fee", total_fee);
        PaymentLog += "total_fee:" + total_fee + ";";
        sParaTemp.Add("paymethod", paymethod);
        PaymentLog += "paymethod:" + paymethod + ";";
        sParaTemp.Add("defaultbank", defaultbank);
        PaymentLog += "defaultbank:" + defaultbank + ";";
        sParaTemp.Add("anti_phishing_key", anti_phishing_key);
        PaymentLog += "anti_phishing_key:" + anti_phishing_key + ";";
        sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);
        PaymentLog += "exter_invoke_ip:" + exter_invoke_ip + ";";
        sParaTemp.Add("extra_common_param", extra_common_param);
        PaymentLog += "extra_common_param:" + extra_common_param + ";";
        sParaTemp.Add("buyer_email", buyer_email);
        PaymentLog += "buyer_email:" + buyer_email + ";";
        sParaTemp.Add("royalty_type", royalty_type);
        PaymentLog += "royalty_type:" + royalty_type + ";";
        sParaTemp.Add("royalty_parameters", royalty_parameters);
        PaymentLog += "royalty_parameters:" + royalty_parameters + ";";

        GZ.Entity.EPaymentLog obj = new GZ.Entity.EPaymentLog();
        obj.InsertLog(3, PaymentLog, Convert.ToInt64(out_trade_no));

        //构造即时到帐接口表单提交HTML数据，无需修改
        Service ali = new Service();
        string sHtmlText = ali.Create_direct_pay_by_user(sParaTemp);
        Response.Write(sHtmlText);
    }
}